﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsumeSys.Orm
{
    /// <summary>
    /// 排序类型
    /// </summary>
    public enum SQLOrderByType
    {
        /// <summary>
        /// 升序
        /// </summary>
        ASC,
        /// <summary>
        /// 降序
        /// </summary>
        DESC
    }

    /// <summary>
    /// 分页操作类
    /// </summary>
    public class Paging<T> where T : new()
    {
        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public Paging()
        {

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_pageIndex">页码</param>
        public Paging(int _pageIndex)
        {
            this._pageIndex = _pageIndex;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_pageIndex">页码</param>
        /// <param name="_pageSize">页大小，默认为50条</param>
        public Paging(int _pageIndex, int _pageSize)
        {
            this._pageIndex = _pageIndex;
            this._pageSize = _pageSize;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_pageIndex">页码</param>
        /// <param name="_tableName">表名称</param>
        public Paging(int _pageIndex, string _tableName)
        {
            this._pageIndex = _pageIndex;
            this._tableName = _tableName;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_pageIndex">页码</param>
        /// <param name="_pageSize">页码页大小</param>
        /// <param name="_tableName">表名称</param>
        public Paging(int _pageIndex, int _pageSize, string _tableName)
        {
            this._pageIndex = _pageIndex;
            if (_pageSize != 0)
            {
                this._pageSize = _pageSize;
            }
            this._tableName = _tableName;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_pageIndex">页码</param>
        /// <param name="_tableName">表名称</param>
        /// <param name="_sqlWhere">查询条件，自动添加【where 1=1】，默认值为【string.Empty】</param>
        public Paging(int _pageIndex, string _tableName, string _sqlWhere)
        {
            this._pageIndex = _pageIndex;
            this._tableName = _tableName;
            this._sqlWhere = _sqlWhere;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_pageIndex">页码</param>
        /// <param name="_pageSize">页大小</param>
        /// <param name="_tableName">表名称</param>
        /// <param name="_sqlWhere">查询条件，自动添加【where 1=1】，默认值为【string.Empty】</param>
        public Paging(int _pageIndex, int _pageSize, string _tableName, string _sqlWhere)
        {
            this._pageIndex = _pageIndex;
            this._pageSize = _pageSize;
            this._tableName = _tableName;
            this._sqlWhere = _sqlWhere;
        }

        #endregion

        #region 分页属性

        private int _pageIndex = 1;
        /// <summary>
        /// 页码，默认为1
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        private int _pageSize = 50;
        /// <summary>
        /// 页大小，默认为50
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (value != 0)
                {
                    _pageSize = value;
                }
            }
        }

        private int _recordCount = 0;
        /// <summary>
        /// 总记录数，默认为0
        /// </summary>
        public int RecordCount
        {
            get { return _recordCount; }
        }

        private int _pageCount = 0;
        /// <summary>
        /// 总页数，默认为0
        /// </summary>
        public int PageCount
        {
            get { return _pageCount; }
        }

        private List<T> _dataList = new List<T>();
        /// <summary>
        /// 分页数据
        /// </summary>
        public List<T> DataList
        {
            get { return _dataList; }
        }

        private string[] _orderByColumnName = new string[] { "id" };
        /// <summary>
        /// 排序列名，默认【id】列
        /// </summary>
        public string[] OrderByColumnName
        {
            set { _orderByColumnName = value; }
        }

        private SQLOrderByType[] _orderByType = new SQLOrderByType[] { SQLOrderByType.DESC };
        /// <summary>
        /// 排序类型，默认降序
        /// </summary>
        public SQLOrderByType[] OrderType
        {
            set { _orderByType = value; }
        }

        private string _tableName = typeof(T).Name;
        /// <summary>
        /// 表名称，默认为当前类类名称
        /// </summary>
        public string TableName
        {
            set { _tableName = value; }
        }

        private string _sqlWhere = string.Empty;
        /// <summary>
        /// 查询条件，自动添加【where 1=1】，默认值为【string.Empty】
        /// </summary>
        public string SqlWhere
        {
            set { _sqlWhere = value; }
        }

        #endregion

        #region 执行查询

        /// <summary>
        /// 获取分页数据
        /// </summary>
        public void GetPageingDataList()
        {
            if (_pageIndex < 1) _pageIndex = 1;

            #region 排序条件

            string _orderby = string.Empty;
            if (_orderByColumnName == null || _orderByColumnName.Length <= 0)
            {
                throw new Exception("至少包含一个排序列！");
            }
            if (_orderByType == null || _orderByType.Length <= 0)
            {
                throw new Exception("至少包含一种排序方式！");
            }
            ArrayList _arryList = new ArrayList();
            for (int i = 0; i < _orderByColumnName.Length; i++)
            {
                _arryList.Add(_orderByColumnName[i] + " " + _orderByType[i].ToString());
            }
            _orderby = string.Join(",", (string[])_arryList.ToArray(typeof(string)));

            #endregion

            ArrayList _arrayList = EntityBase.ExecutePageingSQL<T>(_orderby, _tableName, _sqlWhere, _pageIndex, _pageSize);
            if (_arrayList != null && _arrayList.Count == 4)
            {
                this._pageIndex = int.Parse(_arrayList[0].ToString());
                this._recordCount = int.Parse(_arrayList[1].ToString());
                this._pageCount = int.Parse(_arrayList[2].ToString());
                this._dataList = (List<T>)_arrayList[3];
            }
            else
            {
                throw new Exception("执行分页查询出现异常！");
            }
        }

        #endregion
    }
}
